//
//  SLRecordListVC.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/16.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import SwiftyJSON

class SLRecordListVC: SLTableVC<Any> {

  
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableview.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        self.emptyDataView.isHidden = false
        self.tableview.backgroundColor = .white
        self.tableview.mj_footer = nil
        self.tableview.estimatedRowHeight = 192
        self.tableview.register(SLRecordListCell.self,forCellReuseIdentifier: "SLRecordListCell")
    }
    override func refreshData(isPullDown: Bool = true) {
        self.tableview.mj_footer?.endRefreshing()
        self.tableview.mj_header?.endRefreshing()
        SLHttpTool.AsyncReportData(path: "/satisfies/chunar",params: ["traced":self.id], target: self) { responseCall, success, msg, code in
            if success{
                self.datas.removeAll()
                self.autoAdd(responseCall: responseCall["wavelets"]["quite"],total: responseCall["wavelets"]["metcalf"].intValue, block: { subJson in
                    self.datas.append(subJson)
                }, isPullRefresh: isPullDown)
                DispatchQueue.main.async {
                    self.reloadData()
                }
            }else{
//                MBProgressHUD.showError(error: msg)
            }
        }
    }
    func reloadData(){
        self.tableview.reloadData()
        self.emptyDataView.isHidden = self.datas.count>0
        
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SLRecordListCell", for: indexPath) as! SLRecordListCell
        cell.setupUI()
        let json = self.datas[indexPath.section] as! JSON
        cell.setData(json: json)

        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let json = self.datas[indexPath.section] as! JSON
        let string = json["lumpur"].stringValue
        jump(url:string)
        
    }
  
    override func numberOfSections(in tableView: UITableView) -> Int {

        return self.datas.count
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 12
    }
  
    lazy var emptyDataView:SLNoDataView = {
        let v = SLNoDataView()
        v.backgroundColor = .clear
        view.addSubview(v)
        v.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(250)
        }
        return v
    }()
}

class NoDataView: UIView {
    lazy var imageV:UIImageView = {
        let imageView = UIImageView()
        imageView.image = IMAGE("nodata")
        self.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.width.equalTo(180)
            make.height.equalTo(196)
            make.centerX.equalToSuperview()
            make.top.equalTo(-150)
        }
        return imageView
    }()
    lazy var titleL:UILabel = {
        let label = create_label(text:"No orders yet", textColor: rgba(hex: "#8F8F8F"), textFont: SLFontConfig.regular(size: 16), supView: self) { make in
            make.top.equalTo(self.imageV.snp.bottom).offset(0)
            make.centerX.equalToSuperview()
        }
        return label
    }()
    lazy var applyBtn: UIButton = {
        let button = create_button(title: "Apply loan", titleColor: .white, titleFont: SLFontConfig.bold16, image: nil, supView: self, closure: { make in
            make.width.equalTo(144)
            make.height.equalTo(44)
            make.centerX.equalToSuperview()
            make.top.equalTo(self.titleL.snp.bottom).offset(48)
        }, selector: (target: self, action: #selector(buttonClick), event: .touchUpInside))
        button.layer.cornerRadius = 22
        button.layer.masksToBounds = true
        button.backgroundColor = rgba(hex: "#1608EC")
        return button
    }()
    @objc func buttonClick(){

    }

}
